import request from "@/utils/request";

export default {
  // 产品分类
  category() {
    return request.get("public/productsCategory");
  },
  // 产品列表
  lists(page = 1, size = 10, cid = 0) {
    return request.get("public/products", {
      page,
      size,
      cat: cid
    });
  },
  // 兑换
  exchange(formData) {
    return request.post("coin/buyGood", formData);
  },
  orderList(page = 1, size = 10) {
    return request.get("order/goodOrderList", {
      page,
      size
    })
  },
  // 订单详情
  orderDetail(id) {
    return request.get("order/goodOrderDetail", {
      order_id: id
    })
  },
  // 订单支付结果
  async getOrderResult(id) {
    return request.get("coin/payDetail", {
      id
    }).then(response1 => {
      // 检测到支付成功
      if (response1.data.data.status === 1) {
        console.warn(response1.data)
        return Promise.resolve(response1.data);
      }
      // 检测失败,进入第二轮检测
      setTimeout(function() {
        request.get("coin/payDetail", {
          id
        }).then(response2 => {
          if (response2.data.data.status === 1) {
            return Promise.resolve(response2.data);
          }else{
            return Promise.reject(response2);
          } 
        });
      }, 3000);
    });
  }
};
